// pages/repair/repair.js
var cate=""; //用来保存picker组件选中的类别名称
var cateid="";//用来保存picker组件选中的类别id
var adds=[];
Page({

  /**
   * 页面的初始数据
   */
  data: {
  img_arr: [],  
  campus:1,
  cateArray:[
    {id:'1', cate:'雅安校区'},{id:'2', cate:'温江校区'},{id:'3',cate:'都江堰校区'}],
    cateIndex:0,
  userInfo:'',
  token:'',
  },
 
  formSubmit: function (e) {  
    var id = e.target.id ;
   adds.classroom=e.detail.value.classroom;
   adds.name=e.detail.value.name;
   adds.description=e.detail.value.description;
   adds.campus = this.data.campus;
   adds.user_id = this.data.userInfo.userid;
   this.upload();
  },  

  bindCatePickerChange: function (e) {
    var cname=this.data.cateArray[e.detail.value].cate;
    var cid=this.data.cateArray[e.detail.value].id;
    cateid=cid;
    this.setData({
      cateIndex: e.detail.value,     
      campus:cid,
    }) 
  },
  upload: function () {  
    var that = this  ;
    console.log(adds);
      for (var i=0; i < this.data.img_arr.length; i++) {  
        wx.uploadFile({  
          url: 'http://localhost:8080/classroom_ms1.0/repair',  
          filePath: that.data.img_arr[i],  
          name: 'file',  
          formData: adds,   
          success: function (res) {  
            console.log(res.data)
            var data = JSON.parse(res.data);
            if (data.code==1) {  
              console.log(res.data.msg)
              wx.showToast({  
                title: data.msg,  
                duration: 2000  
              });  
            }else{
              wx.showToast({  
                title: data.msg,  
                duration: 2000  
              });  
              setTimeout(function() {
              wx.navigateBack({
                delta: 1,
              })
             }, 2000);
            }
          }  
        })  
      }  

  }, 

  upimg: function () {  
    var that = this;  
    if (this.data.img_arr.length<1){  
    wx.chooseImage({  
      sizeType: ['original', 'compressed'],  
      success: function (res) {  
        that.setData({  
          img_arr: that.data.img_arr.concat(res.tempFilePaths)  
        })  
      }  
    })  
    }else{  
      wx.showToast({  
        title: '最多上传一张图片',  
        icon: 'loading',  
        duration: 3000  
      });  
    }  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var cindex=this.data.cateIndex
    cate=this.data.cateArray[cindex].cate;
    cateid=this.data.cateArray[cindex].id;
    var userInfo = wx.getStorageSync('userInfo');
    var token = wx.getStorageSync('token');
    if(userInfo!=''){
      console.log(userInfo);
    this.setData({
      userInfo:JSON.parse(userInfo),
      token:token,
    })
  }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})